package com.order.controller.business;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.order.config.Consts;
import com.order.model.Account;
import com.order.model.Cuisine;
import com.order.utils.CodeUtil;

/** 
* @类名: IndexController 
* @描述: 客户端默认控制器
* @作者: 林勃 
* @时间: 2014年4月22日 下午10:51:25 
*  
*/ 
@ControllerBind(controllerKey = "/")
public class IndexController extends Controller {

	private Logger log = LoggerFactory.getLogger(IndexController.class);
	/** 
	* @Title: index 
	* @描述:    定位首页页面 
	*/ 
	public void index(){

		List<Cuisine> list = Cuisine.dao.getCuisineDish();
		setAttr("cuisine", list);
		System.out.println(list);
		render(Consts.BUSINESS + "index.htm");
	}
	
	/** 
	* @Title: toFrLogin 
	* @Description:    定位登录模块页面
	*/ 
	public void toFrLogin(){
		render(Consts.BUSINESS + "fr_login.htm");
	}
	
	/** 
	* @Title: toLogin 
	* @Description:    定位登录页面
	*/ 
	public void toLogin(){
		render(Consts.BUSINESS + "login.htm");
	}
	
	/** 
	* @Title: toRegister 
	* @描述:    定位注册页面
	*/ 
	public void toRegister(){
		render(Consts.BUSINESS + "register.htm");
	}
	
	/** 
	* @Title: toModifyAccountMsg 
	* @描述:    定位客户信息修改页面
	*/ 
	public void toModifyAccountMsg(){
		Account account = (Account)getSessionAttr(Consts.ACCOUNT);
		setAttr("account", account);
		render(Consts.BUSINESS + "accountMsg.htm");
	}
	/** 
	* @Title: getCode 
	* @描述:    获取验证码
	*/ 
	public void getCode(){
		try {
			OutputStream os = getResponse().getOutputStream();
			HttpSession session = getRequest().getSession();
			CodeUtil.getCode(os, session);
			renderNull();
		} catch (IOException e) {
			e.printStackTrace();
		}
		/*render(new CaptchaRender("test"));*/

	}
	
	/** 
	* @Title: logout 
	* @描述:    登出
	*/ 
	public void logout(){
		setSessionAttr(Consts.ACCOUNT, null);
		redirect("/");
	}
	
	/** 
	* @Title: loginSubmit 
	* @Description:    登录处理
	*/ 
	public void loginSubmit(){
		String name = null;
		try {
			name = URLDecoder.decode(getPara("name", "").trim(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String password =  getPara("password","");
		String codeI = getPara(Consts.CODE);
		String code = getSessionAttr(Consts.CODE);
		log.info("客户认证开始:" + name + " , " + password); 
		setAttr("msg", "success");
		Account account = null;
		if(codeI.equalsIgnoreCase(code)){
			if(Account.dao.isAccountEnabled(name)){
				account = Account.dao.getAcount(name, password);
				if(account == null){
					setAttr("msg", "用户名或密码错误");
					log.info("用户名或密码错误");
				}else{
					HttpSession session = getRequest().getSession();
					session.setAttribute(Consts.ACCOUNT, account);
					log.info("认证成功，并添加到session中");
				}
			}else{
				setAttr("msg", "账号已被加入黑名单");
				log.info("账号已被加入黑名单");
			}
		}else{
			setAttr("msg", "验证码错误");
			log.info("验证码错误");
		}
		log.info("客户认证完毕:" + name);
		//清除当前session的code
		setSessionAttr(Consts.CODE, "");
		String lastURL = getSessionAttr(Consts.LAST_URL);
		if(StringKit.notBlank(lastURL)){
			if(account != null){
				log.info("跳转到登录前的访问路径url：" + lastURL);
				setAttr("lastURL", lastURL);
			}else{
				log.info("客户认证失败，重定向到登录页面");
				setAttr("lastURL", "/toLogin");
			}
		}
		renderJson();
	}
	
	/** 
	* @Title: getAllCuisineDish 
	* @描述:    获取菜系菜品信息
	*/ 
	public void getAllCuisineDish(){
		List<Cuisine> list = Cuisine.dao.getCuisineDish();
		setAttr("cuisine", list);
		renderJson();
	}
	
	/** 
	* @Title: register 
	* @描述:    注册用户
	*/ 
	public void register(){
		String name = getPara("name");
		String password = getPara("password");
		String gender = getPara("gender");
		String phone = getPara("phone");
		String address = getPara("address");
		String mail = getPara("mail", "");
		String qq = getPara("qq", "");
		String identity = getPara("identity", "");
		Account account = new Account();
		account.set(Account.NAME, name);
		account.set(Account.PASSWORD, password);
		account.set(Account.PHONE, phone);
		account.set(Account.GENDER, gender);
		account.set(Account.ADDRESS, address);
		account.set(Account.MAIL, mail); 
		account.set(Account.IDENTIFY, identity);
		account.set(Account.QQ, qq);
		account.save();
		redirect("/toLogin");
	}
	
	/** 
	* @Title: checkAccountName 
	* @Description:    检查客户名是否重复
	*/ 
	public void checkAccountName(){
		String name = getPara("accountName");
		boolean ok = Account.dao.isNameEnable(name);
		renderJson(ok);
	}
	
	/** 
	* @Title: modifyAccountMsg 
	* @描述:    修改客户信息
	*/ 
	public void modifyAccountMsg(){
		String name = getPara("name");
		String password = getPara("password");
		String gender = getPara("gender");
		String phone = getPara("phone");
		String address = getPara("address");
		String mail = getPara("mail", "");
		String qq = getPara("qq", "");
		String identity = getPara("identity", "");
		boolean result = false;
		if(StringKit.notBlank(name) &&
				StringKit.notBlank(password) &&
				StringKit.notBlank(phone) &&
				StringKit.notBlank(address)){
			Account account = getSessionAttr(Consts.ACCOUNT);
			account.set(Account.PASSWORD, password);
			account.set(Account.PHONE, phone);
			account.set(Account.GENDER, gender);
			account.set(Account.ADDRESS, address);
			account.set(Account.MAIL, mail); 
			account.set(Account.IDENTIFY, identity);
			account.set(Account.QQ, qq);
			account.update();
			result = true;
			setSessionAttr(Consts.ACCOUNT, null);
		}
		renderJson(result);
	}
}
